//
//  AudioViewController.m
//  Project_A
//
//  Created by running on my way on 15/1/16.
//  Copyright (c) 2015年 Flowers. All rights reserved.
//

#import "AudioViewController.h"
#import "AFNetworking.h"
@interface AudioViewController ()

@end

@implementation AudioViewController
//内存管理
- (void)dealloc
{
    [_dataArray release];
    [_tableView release];
    [_type release];
    [super dealloc];
}
//在view将要显示的时候改变tableBar 的隐藏属性为no,让它显示
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
// 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    //请求数据
    [self AFN];
    
}
//cell的重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentfieir = @"resue";
    AudioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentfieir];
    if (cell == nil) {
        cell = [[AudioTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentfieir];
        
    }
    //给cell 上添加内容
    AudioModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
//cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
//cell 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//触发事件方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioPlayerViewController *playerView = [[AudioPlayerViewController alloc] init];
    //将本页得到的图片地址通过属性唱给下级页面
    AudioModel *model = [self.dataArray objectAtIndex:indexPath.row];
    playerView.PicUrl = model.audioBigPic;
    playerView.largePic = model.audioPic;
    //将所有的音频地放在一个数组中,在触发cell方法的时候传给下级页面
    playerView.urlArray = self.urlArray;
    //index 作为数组中元素的下标 让它等于indexpath.row 为了控制点击哪个cell,传哪个cell对应的音频地址
    playerView.index = indexPath.row;
  [self.navigationController pushViewController:playerView animated:YES];
}
//数据请求
- (void)AFN
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://www.thatbbs.com/newAudio/api/getAlbumIdAudioList.do";
    NSDictionary *dic = @{@"albumId":self.type};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    //用来装model的数组
      self.dataArray = [NSMutableArray array];
    //用来装音频地址的数组
    self.urlArray = [NSMutableArray array];

    __block AudioViewController *own = self;
    [manager POST:url_string parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
       // NSLog(@"%@", responseObject);
        NSDictionary *dic = responseObject[@"response"];
        NSArray *array = dic[@"ReturnData"];
                for (NSDictionary *dataDic in array) {
            AudioModel *model = [[AudioModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
                    //将model装到数组中
            [own.dataArray addObject:model];
        //将64位音频的播放地址全装到一个数组中
          [own.urlArray addObject:model.audioPlay64];
           //内存管理
            [model release];
        }
        //刷新tableView
        [own.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
